package com.sunlands.zlcx.usercenter.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sunlands.zlcx.usercenter.constant.YN;
import com.sunlands.zlcx.usercenter.domain.UsersDO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import static com.sunlands.zlcx.usercenter.domain.UsersDO.StatusEnums.LIVE_LEARNING;

/**
 * @author anjl
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "用户（道友）")
public class UsersVO implements Serializable {
    private static final long serialVersionUID = 5870897365131736427L;
    @ApiModelProperty(position = 10, value = "id")
    private Long id;

    @ApiModelProperty(position = 20, value = "姓名(昵称)")
    private String name;

    @ApiModelProperty(position = 30, value = "账号")
    private String username;

    @ApiModelProperty(position = 40, value = "手机号")
    private String phone;

    @ApiModelProperty(position = 50, value = "年龄")
    private Integer age;

    @JsonIgnore
    private String password;

    @ApiModelProperty(position = 60, value = "性别 0 男 1 女")
    private Integer sex;

    @ApiModelProperty(position = 70, value = "生日")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date birthday;

    @ApiModelProperty(position = 80, value = "身份(primary_and_middle_school 中小学| college_student 大学生| office_worker 上班族 |liberals 自由派)")
    private String identity;

    @ApiModelProperty(position = 80, value = "身份名称")
    private String identityName;

    @ApiModelProperty(position = 81, value = "用户类型（1真实用户 -1虚拟用户 2信息不完整用户）")
    private Integer type;

    @ApiModelProperty(position = 90, value = "微信unionId")
    private String unionId;

    @ApiModelProperty(position = 90, value = "app获取到的openid")
    private String appOpenId;

    @ApiModelProperty(position = 100, value = "用户头像")
    private String avatarUrl;

    @ApiModelProperty(position = 105, value = "背景图")
    private String backgroundUrl;

    @ApiModelProperty(position = 110, value = "设备id")
    private String deviceId;

    @ApiModelProperty(position = 120, value = "学习目标id")
    private Long currentLearnTargetId;

    @ApiModelProperty(position = 130, value = "学习目标")
    private String currentLearnTargetName;

    @ApiModelProperty(position = 140, value = "用户状态,1:未学习,2:学习计时中，3:连麦学习中,12:学习计时暂停中,13:学习计时继续")
    /**
     *  {@link com.sunlands.zlcx.usercenter.constant.LearningStatus}
     */
    private Integer status;

    @ApiModelProperty(position = 150, value = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updateTime;

    @ApiModelProperty(position = 150, value = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;

    /**
     * 开始学习时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty(value = "开始学习时长")
    private Date startLearnTime;

    @ApiModelProperty(position = 170, value = "个人主页使用：道友人数")
    private Integer friendCount;

    @ApiModelProperty(position = 160, value = "个人主页使用：粉丝数量")
    private Integer fansCount;

    @ApiModelProperty(position = 170, value = "个人主页使用：关注人数")
    private Integer followCount;

    @ApiModelProperty(position = 171, value = "个人主页使用：是否为道友 0不是 1是 2不是道友，但已发送好友请求 -1自己")
    private Integer isFriend;

    @ApiModelProperty(position = 171, value = "个人主页使用：是否为该用户的粉丝 0不是 1是(如果已经是道友，不显示关注) -1自己")
    private Integer isFans;

    @ApiModelProperty(position = 180, value = "学习时长")
    private BigDecimal studyHours;

    @ApiModelProperty(position = 190, value = "目标天数")
    private Integer targetDays;

    @ApiModelProperty(position = 191, value = "发布日记数量")
    private Long diaryCount;
    @ApiModelProperty(position = 192, value = "学习数量")
    private Integer studyCount;

    @ApiModelProperty(position = 200, value = "用户是否登录（0未登录，1登录）")
    private Integer loggedIn;
    @ApiModelProperty(position = 201, value = "免打扰 1 开启 0关闭")
    private Integer disturb;

    @ApiModelProperty(value = "是否vip用户，1是，0不是")
    private Integer vipStatus;

    @ApiModelProperty(position = 201, value = "用户等级 1. 安全用户 2. 待考察用户 3. 风险用户")
    private Integer leave;

    @ApiModelProperty(position = 202, value = "上麦id")
    private Long lianMaiId;

    @ApiModelProperty(position = 204, value = "房间id")
    private Long roomId;

    @ApiModelProperty(position = 205, value = "房间类型100、中小学 200、高中 300、大学 400、在职")
    private Integer roomType;

    /**
     * {@link com.sunlands.zlcx.usercenter.constant.BlackEnum}
     */
    @ApiModelProperty(position = 205, value = "黑名单标识：1您已将该用户放至黑名单，2当前用户已把你放至黑名单")
    private Integer blackStatus;

    @ApiModelProperty(position = 210, value = "用户是否在线1、在线 0、不在线")
    private Integer online;

    @ApiModelProperty(position = 211, value = "个性签名")
    private String personalSign;

    @ApiModelProperty(position = 212, value = "所在省")
    private String province;

    @ApiModelProperty(position = 213, value = "所在市")
    private String city;

    @ApiModelProperty(position = 214, value = "学校")
    private String school;

    @ApiModelProperty(position = 305, value = "是否绑定过手机号 1绑定过 0 未绑定过")
    private Integer bingingPhone;

    @ApiModelProperty(position = 305, value = "用户socket通道id（创建时间）")
    private Long channelId;

    /**
     * tongzhuo,postgraduate
     */
    private String app;

    public static UsersVO from(UsersDO users) {
        return Objects.isNull(users) ? null : UsersVO.builder().id(users.getId())
                .age(getAgeByBirth(users.getBirthday()))
                .avatarUrl(users.getAvatarUrl())
                .backgroundUrl(users.getBackgroundUrl())
                .birthday(users.getBirthday())
                .name(users.getName())
                .unionId(users.getUnionId())
                .appOpenId(users.getAppOpenId())
                .sex(users.getSex())
                .password(users.getPassword())
                .type(Objects.isNull(users.getType()) ? 1 : users.getType())
                .identity(users.getIdentity())
                .identityName(UsersDO.IdentityEnums.covert(users.getIdentity()))
                .phone(users.getPhone())
                .username(users.getUsername())
                .deviceId(users.getDeviceId())
                .createTime(users.getCreateTime())
                .updateTime(users.getUpdateTime())
                .currentLearnTargetId(users.getCurrentLearnTargetId())
                .currentLearnTargetName(users.getCurrentLearnTargetName())
                .startLearnTime(users.getStartLearnTime())
                .status(LIVE_LEARNING.code.equals(users.getStatus()) && Objects.isNull(users.getRoomId()) ? Integer.valueOf(1) : users.getStatus() )
                .loggedIn(users.getLoggedIn())
                .disturb(Objects.nonNull(users.getDisturb()) ? users.getDisturb() : 0)
                .leave(Objects.isNull(users.getLeave()) ? 2 : users.getLeave())
                .lianMaiId(users.getLianMaiId())
                .roomId(users.getRoomId())
                .vipStatus(Objects.isNull(users.getVipStatus()) ? YN.NO.getCode() : users.getVipStatus())
                .online(Objects.isNull(users.getOnline()) ? YN.NO.getCode() : users.getOnline())
                .personalSign(users.getPersonalSign())
                .province(users.getProvince())
                .city(users.getCity())
                .school(users.getSchool())
                .bingingPhone(Objects.isNull(users.getBingingPhone()) ? YN.NO.getCode() : users.getBingingPhone())
                .channelId(users.getChannelId())
                .app(users.getApp())
                .build();
    }

    public static int getAgeByBirth(Date birthDay) {
        int age = 0;
        Calendar cal = Calendar.getInstance();
        //出生日期晚于当前时间，无法计算
        if (Objects.isNull(birthDay) || cal.before(birthDay)) {
            return age;
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
        //计算整岁数
        age = yearNow - yearBirth;
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                }
            } else {
                age--;
            }
        }
        if (age < 0) {
            return 0;
        }
        return age;
    }

}
